/*
 * Window.cpp
 */
#include "Window.h"
#include "ComponentManager.h"

namespace gui
  {
    int Window::_titleBarHeight = 14;
    
    Window::Window()
      {
        _previouseMouseB = _xOffset = _yOffset = 0;
        _moveable = true;
        _title = "Untitled Window";
      }

    void Window::init()
      {
        bgGc().mode(DRAW_MODE_TRANS);
        bgGc().alpha(130);

        fgGc().mode(DRAW_MODE_SOLID);

        borderGc().mode(DRAW_MODE_SOLID);
      }

    void Window::process()
      {
        checkDragging();
      }
    
    void Window::paint(BITMAP* buffer, int x_offset, int y_offset)
      {
        int x1 = x_offset + x();
        int y1 = y_offset + y();
        int x2 = x1 + width();
        int y2 = y1 + height();

        bgGc().activate();

        rectfill(buffer, x1, y1, x2, y2, 0x0); 
        
        paintChildren(buffer, x_offset, y_offset);
        paintBorder(buffer, x_offset, y_offset);
      }

    void Window::paintBorder(BITMAP* buffer, int x_offset, int y_offset)
      {
        int x1, y1, x2, y2;
        int size = 3;
        int size_top = 20;

        borderGc().activate();

        // window border
        x1 = x_offset + x() -1;
        y1 = y_offset + y() -1;
        x2 = x_offset + x() + width() +2;
        y2 = y_offset + y() + height() +2;
        rect(buffer, x1, y1, x2, y2, 0x222222);

        x1 = x_offset + x() -2;
        y1 = y_offset + y() -2;
        x2 = x_offset + x() + width() +1;
        y2 = y_offset + y() + height() +1;
        rect(buffer, x1, y1, x2, y2, 0xaaaaaa);

        bgGc().activate();

        // title bar
        x1 = x_offset + x();
        y1 = y_offset + y() - _titleBarHeight;
        x2 = x_offset + x() + width();
        y2 = y_offset + y() -1;
        rectfill(buffer, x1, y1, x2, y2, makecol(0,21,82));

        fgGc().activate();
        
        x1 = x_offset + x() + width() * 0.5;
        y1 = y_offset + y() - _titleBarHeight + 2;
        textout_centre_ex(buffer, font, _title.c_str(), x1, y1, 0xffffff, -1);
        
        borderGc().activate();

        // title bar border
        x1 = x_offset + x() -1;
        y1 = y_offset + y() - _titleBarHeight -1;
        x2 = x_offset + x() + width() +2;
        y2 = y_offset + y() -1;
        rect(buffer, x1, y1, x2, y2, 0x222222);

        x1 = x_offset + x() -2;
        y1 = y_offset + y() - _titleBarHeight -2;
        x2 = x_offset + x() + width() +1;
        y2 = y_offset + y() -2;
        rect(buffer, x1, y1, x2, y2, 0xaaaaaa);
        
        
      }

    bool const& Window::moveable() const { return _moveable; }
    
    bool const& Window::moveable(bool b) { return _moveable = b; }

    GraphicContext& Window::bgGc() { return _bgGc; }
    GraphicContext& Window::fgGc() { return _fgGc; }
    GraphicContext& Window::borderGc() { return _borderGc; }
    
    void Window::checkDragging()
      {
        int min_x, min_y, max_x, max_y;
        int x, y;

        min_x = 0;
        min_y = _titleBarHeight;

        if (parent() != NULL)
          {
            max_x = parent()->width();
            max_y = parent()->height();
          }
        else
          {
            max_x = SCREEN_W;
            max_y = SCREEN_H;
          }
        max_x -= this->width();
        max_y -= this->height();
        
        if (mouse_b)
          {
            if (!_previouseMouseB)
              {
                _xOffset = mouse_x - this->x();
                _yOffset = mouse_y - this->y();
              }
            else
              {
                if (hasFocus() && moveable())
                  {
                    x = mouse_x - _xOffset;
                    y = mouse_y - _yOffset;
                    if (x > max_x) x = max_x;
                    if (y > max_y) y = max_y;
                    if (x < min_x) x = min_x;
                    if (y < min_y) y = min_y;
                    move(x, y);
                  }
              }
          }
        _previouseMouseB = mouse_b;
      }

      std::string const& Window::title() const { return _title; }
      std::string const& Window::title(std::string title) 
        {
          return _title = title;
        };
  }

